#include <algorithm>
#include <array>
#include <cassert>
#include <cmath>
#include <cstring>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <optional>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

using big = long long;

const int N = 1020;
big a[N][N];
big s[N][N];
big s1[N][N];
big s2[N][N];
big f[N][N], g[N][N];
int main() {
    cin.tie(0);
    cout.tie(0);
    ios_base::sync_with_stdio(false);
    int n, m, n1, m1, n2, m2;
    cin >> n >> m >> n1 >> m1 >> n2 >> m2;
    n2 = min(n2, n1);
    m2 = min(m2, m1);
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= m; ++j) {
            cin >> a[i][j];
        }
    }
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= m; ++j) {
            s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + a[i][j];
        }
    }
    for (int i = n1; i <= n; ++i) {
        for (int j = m1; j <= m; ++j) {
            s1[i][j] = s[i][j] - s[i - n1][j] - s[i][j - m1] + s[i - n1][j - m1];
        }
    }
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= m; ++j) {
            s2[i][j] = s[i][j];
            if (i >= n2) {
                s2[i][j] -= s[i - n2][j];
            }
            if (j >= m2) {
                s2[i][j] -= s[i][j - m2];
            }
            if (i >= n2 && j >= m2) {
                s2[i][j] += s[i - n2][j - m2];
            }
        }
    }
    for (int i = 1; i <= n; ++i) {
        multiset<big> s;
        for (int j = 1; j <= m; ++j) {
            s.insert(s2[i][j]);
            if (j > (m1 - m2 + 1)) {
                auto it = s.find(s2[i][j - (m1 - m2 + 1)]);
                s.erase(it);
            }
            f[i][j] = *s.rbegin();
        }
    }
    for (int j = 1; j <= m; ++j) {
        multiset<big> s;
        for (int i = 1; i <= n; ++i) {
            s.insert(f[i][j]);
            if (i > n1 - n2 + 1) {
                auto it = s.find(f[i - (n1 - n2 + 1)][j]);
                s.erase(it);
            }
            g[i][j] = *s.rbegin();
        }
    }
    big ans = 0;
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= m; ++j) {
            ans = max(ans, s1[i][j] - g[i][j]);
        }
    }
    cout << ans << endl;
}